SETUP - loading required packages

Load in data

TOPPID <- "2021044"
SEALID <- "test33"
filename <- paste("02_00_SLEEP_",TOPPID,"_",SEALID,"_10_NewRaw.csv",sep="")

Data <- read_csv(here("data",filename))
## Rows: 51490 Columns: 33
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (8): Sleep_Code, R_Time, Resp_Code, Water_Code, SealID, Recording_ID, I...
## dbl (25): Sleep_Num, Seconds, Resp_Num, Water_Num, Simple_Sleep_Num, pitch, ...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Plotting depth versus time for a single northern elephant seal adult female for animation

This code plots the tracks of all female northern elephant seals from our study

DailyData <- read_csv(here("data","02_02_AdultFemaleData_DailySleepEstimates_wide_goodtracks.csv"))
## Rows: 48205 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (1): SEALID
## dbl (12): TOPPID, unique_Days, Days_Elapsed, Percent_of_Trip, daily_recordin...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
num_unique <- length(unique(DailyData$SEALID))

tracks <- ggplot(DailyData) +
  geom_path(aes(x = Lon360, y = Lat),
            size = 1, color="lightblue", alpha = 0.05) +
  xlab("Longitude") +
  ylab("Latitude") +
  ggtitle(paste("Tracking data for adult female elephant seals N =",num_unique))+
  coord_quickmap()+
  theme(panel.background = element_blank(),
        panel.grid = element_blank(),
        plot.background = element_blank())
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
tracks

DailyData_subset <- DailyData %>% 
  filter(TOPPID == 2011020 | TOPPID ==2011034)

tracks_subset <- ggplot(DailyData_subset) +
  geom_path(aes(x = Lon360, y = Lat),
            size = 1, color="lightblue", alpha = 1) + # use lower opacity for multiple tracks
  xlab("Longitude") +
  ylab("Latitude") +
  ggtitle(paste("Tracking data for adult female elephant seals N =",num_unique))+
  coord_quickmap()+
  theme(panel.background = element_blank(),
        panel.grid = element_blank(),
        plot.background = element_blank())

tracks_subset

# Export plot as png with transparent background
ggsave(plot=tracks, 
       here("figures","tracks.pdf"), 
       bg = "transparent",limitsize=FALSE,
       width=10,
       height=4)

Use this next chunk when your computer has some time :) This will generate and save your animation

animate(tracks_subset + transition_reveal(DailyData_subset$Days_Elapsed),
        duration=30,fps = 24, width = 1920, height = 1080, bg = 'transparent')

anim_save(here("figures",paste("Tracks_animated.gif", sep="")),
          bg = "transparent")
# For geospatial data, you may not be able to plot your data on a cartesian coordinate system, you may need a specific geospatial projection. This code turns your data into a geospatial "sf" object that stores lat & long for geospatial plotting

DailyData_sf <- st_as_sf(DailyData_subset, coords = c("Long", "Lat"), crs = 4326)

StarryNapMap <- mapview(DailyData_sf, 
        cex = "daily_filtered_long_drift_long_SI",# scale dot size with sleep estimate 
        map.types = "Esri.WorldImagery", # use Esri World Imagery basemap 
        zcol = "daily_filtered_long_drift_long_SI", # color according to sleep estimate
        col.regions = brewer.pal(9, "YlOrRd"), # use color brewer palette YlOrRd
        col = "white", # outline dots with white
        layer.name = "Daily Sleep Estimate") # name the legend
## Warning: Found less unique colors (9) than unique zcol values (270)! 
## Interpolating color vector to match number of zcol values.
StarryNapMap
mapshot(StarryNapMap,
  url = NULL,
  file = here("Figures","StarryNapMap.pdf"),
  remove_controls = c("zoomControl", "layersControl", "homeButton", "scaleBar",
    "drawToolbar", "easyButton")
)
# Plotting using this new sf object (and coord_sf) as opposed to coord_quickmap

# import world map dataset
world_map <- ne_countries(scale = "medium", returnclass = "sf")

# Get the extent of DailyData_sf
bb <- st_bbox(DailyData_sf)

# Create the ggplot object with DailyData_sf and basemap_sf
tracks_sf <- ggplot() +
                # Add the basemap
                geom_sf(data = world_map, fill = "beige", color = "lightgrey") +
                # Add the DailyData points
                geom_sf(data = DailyData_sf, size = 1, color="lightblue", 
                        alpha = 1) +
                # Set the map extent to the extent of DailyData_sf
                coord_sf(xlim = c(bb["xmin"], bb["xmax"]), 
                         ylim = c(bb["ymin"], bb["ymax"]), expand = FALSE) +
                # Add labels and title
                xlab("Longitude") +
                ylab("Latitude") +
                ggtitle(paste("Tracking data for adult female seals, N =",num_unique)) +
                theme(panel.background = element_blank(), panel.grid = element_blank(),
                      plot.background = element_blank())
tracks_sf

# 3D plot of elephant seal sleeping dive using plotly

SleepingDive <- read_csv(here("data","01_02_AnimationExcerpt_Hypnotrack_1Hz.csv"))
## Rows: 3083 Columns: 27
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (7): SealID, Recording_ID, ID, Sleep_Code, Simple_Sleep_Code, Resp_Cod...
## dbl  (19): Seconds, DN, pitch, roll, heading, x, y, z, geoX, geoY, Depth, sp...
## dttm  (1): R_Time
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
simple.sleep.col = c("Unscorable"="#D7D7D7", 
                     "Active Waking"= "#0c2c84",
                     "Quiet Waking" = "#225ea8",
                     "Drowsiness"="#BBA9CF",
                     "REM" = "#FCBE46", 
                     "SWS" = "#41b6c4")

plot_ly(SleepingDive, x = ~x, y = ~y, z = ~z, 
        color = ~Simple_Sleep_Code, 
        colors = simple.sleep.col,
        size = 10,
        type = "scatter3d", mode = "markers")
# 3D plot of elephant seal sleeping dive using plotly

plot_ly(SleepingDive, x = ~x, y = ~y, z = ~z, 
        color = ~roll,
        size = 10,
        type = "scatter3d", mode = "markers")
# Static 3D plot (not interactive) of elephant seal sleeping dive

splot3d <- scatterplot3d(SleepingDive$x, 
              SleepingDive$y, 
              SleepingDive$z,
              color = simple.sleep.col[SleepingDive$Simple_Sleep_Code], 
              type = "p", main = "3D plot of sleeping dives")